class_name Player
extends Node2D

@export var statsSrc: PlayerStatsSrc:
	set = setStats

@onready var sprite: Sprite2D = $Sprite2D
@onready var stats: Stats = $Stats

func _ready():
	Game.player = self

func setStats(value: PlayerStatsSrc) -> void:
	statsSrc = value.instance()
	if !statsSrc.statsChanged.is_connected(renderStats):
		statsSrc.statsChanged.connect(renderStats)
	initStats()

func initStats() -> void:
	if !statsSrc is PlayerStatsSrc: return push_error('状态信息不是玩家的')
	if !is_inside_tree(): await ready
	sprite.texture = statsSrc.skin
	# statsSrc.energy=1
	renderStats()

func renderStats() -> void:
	stats.render(statsSrc)

func getHurt(hurt: int) -> void:
	if statsSrc.health <= 0: return

	await Shaker.shake(self, 16, 0.1)
	statsSrc.getHurt(hurt)

	if statsSrc.health <= 0: Events.died.emit()
